package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.item.client.ItemFeignClient;
import com.atguigu.gmall.model.product.SkuInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

@Controller
public class ItemController {

    @Autowired
    private ItemFeignClient itemFeignClient;

    //商品详情页面  请求地址：http://item.gmall.com/100000121.html
    @GetMapping("/{skuId}.html")
    public String getItem(@PathVariable Long skuId, Model model){

        Map map = itemFeignClient.getItem(skuId);

        Object o =  map.get("skuInfo");
        ObjectMapper objectMapper = new ObjectMapper();
        SkuInfo skuInfo = objectMapper.convertValue(o, SkuInfo.class);
        if (skuInfo.getId() == null){
            map = itemFeignClient.getItem(1L);
        }

        model.addAllAttributes(map);

        return "item/index";

    }

}
